#include<iostream>
using namespace std;

int power_int(int a,int n);
int radReverse(int N,int D);
bool isPrime(int N);
bool isReversePrime(int N,int D);

int main(int argc, char const *argv[])
{
    freopen("test.txt","r",stdin);
    while(1)
    {
        int N,D;
        scanf("%d",&N);
        if(N<0) break;
        scanf("%d",&D);
        if(isReversePrime(N,D))
        {
            printf("Yes\n");
        }
        else printf("No\n");
    }
    return 0;
}

bool isReversePrime(int N,int D)
{
    bool res = false;
    if(isPrime(N) && isPrime(radReverse(N,D)))
    {
        res = true;
    }
    return res;
}


bool isPrime(int N)
{
    bool res = N==1? false: true;
    int i = 2;
    while(i*i <= N)
    {
        if( !(N%i) )
        {
            res = false;
            break;
        }
        i++;
    }
    return res;
}

int radReverse(int N,int D)
{
    int res=0;
    int cnt=1;  // counting the bits
    while(power_int(D,cnt) - 1 < N) cnt++; 

    while(N)
    {
        res += N%D *power_int(D,--cnt);
        N/=D;    
    }
    return res;
}

int power_int(int a,int n)
{
    int res = 1;
    for(int i = 0;i<n;++i)
    {
        res*=a;
    }
    return res;
}